package com.bw.test;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class Time1 {
    public static void main(String[] args) throws  Exception{
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        LocalDateTime time = LocalDateTime.parse("2012-01-02 11:00",dtf);
        System.out.println(time);
        //把时间转换成时间戳    要加上时区    如果是北京时间的话加上8个小时
        Instant instant = time.toInstant(ZoneOffset.ofHours(8));
        System.out.println(instant);

        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        Instant instant2 = now.toInstant(ZoneOffset.ofHours(8));
        System.out.println(instant2.toEpochMilli());
        long a = instant2.toEpochMilli();
        System.out.println("系统时间戳:"+System.currentTimeMillis());
        //转换成时间
        Date d = new Date(a);
        System.out.println(d);
        LocalDateTime ldt = LocalDateTime.ofInstant(d.toInstant(), ZoneOffset.ofHours(8));
        System.out.println(ldt);
    }
}
